*********************************************************************************
* Reproducing Tables and Graphs from Result Section								*
*********************************************************************************
	
* Project: 	National identity, turnout, and party preference: Evidence from Germany
* Authors: 	Matthias Mader, Maria Pesthy, Harald Schoen
* Date: 	December 2018	
	
	
	
	use "Data_final.dta", clear
	
	set scheme plotplainblind
	graph set window fontface "Times New Roman"
	* ssc install addplot


*********************************************************************************
* Table 1																		*
*********************************************************************************


* Logit regressions

	*2015
	logit turnout_rs ethn_fs1 civ_fs1 pint_rs ib2.educat female ost age10 tra uni ben sec con [pweight = weight] if survey == 6 
	estimate store m1

	logit turnout_rs ethn_fs1 civ_fs1 pint_rs pi_rs_d ib2.educat female ost age10 tra uni ben sec con [pweight = weight] if survey == 6 
	estimate store m2


	*2017
	logit turnout_wkp ethn_fs1 civ_fs1 pint_wkp ib2.educat female ost age10 tra uni ben sec con [pweight = weight] if survey == 1 
	estimate store m3

	logit turnout_wkp ethn_fs1 civ_fs1 pint_wkp pi_wkp_d ib2.educat female ost age10 tra uni ben sec con [pweight = weight] if survey == 1 
	estimate store m4
	
	esttab m1 m2 m3 m4 using "Table_1.rtf", replace 	/// 
				stats(r2_p N,fmt(2 0) labels("Pseudo-R2" "N")) ///
				cells(b(star fmt(2)) se(par fmt(2) )) ///
				nonumbers mtitles("A1""A2""B1""B2") ///
				drop(2.educat) ///
				varlabels ///
					 (ethn_fs1 "Ethnic norms" ///
					 civ_fs1 "Civic norms" ///
					 pi_rs_d "Party identification" /// 
					 pi_wkp_d "Party identification" /// 		
					 pint_rs "Political interest" ///
					 pint_wkp "Political interest" ///
					 1.educat "Low education" ///
					 3.educat "High education" ///
					 female "Female" ///
					 ost "East German" ///
					 age10 "Age (in decades)" ///
					 uni "Universalism" ///
					 tra "Traditionalism" ///
					 ben "Benevolence" ///
					 con "Conformity" ///
					 sec "Security" ///
					 _cons "Constant") ///
				order(ethn_fs1 civ_fs1 pint_rs pint_wkp pi_rs_d pi_wkp_d 1.educat 3.educat female ost age10 ben uni tra sec con _cons ) ///
				addnotes("Entries are logit coefficients with standard errors in parentheses from logistic regressions") ///
				varwidth(20) modelwidth(10)	

*********************************************************************************
* Figure 1																		*
*********************************************************************************

	* Predicted probabilities 2015
		estimates restore m1
		margins, at(ethn_fs1=(0 (.1) 1)) post
		estimates store marg1

		estimates restore m1
		margins, at(civ_fs1=(0 (.1) 1)) post
		estimates store marg2
	
	* Predicted probabilities 2016/7
		estimates restore m3
		margins, at(ethn_fs1=(0 (.1) 1)) post
		estimates store marg3

		estimates restore m3
		margins, at(civ_fs1=(0 (.1) 1)) post
		estimates store marg4

	* Coef-Plots
	
		coefplot 	(marg1, msymbol(circle) msize(large) ciopts(recast(rbar) barwidth(0.05) ) offset(.075)) ///
					(marg3, msymbol(diamond) msize(large) ciopts(recast(rbar) barwidth(0.05) ) offset(-.05)) ///
					||(marg2, msymbol(circle) msize(large) ciopts(recast(rbar) barwidth(0.05)) offset(.075)) ///
					(marg4, msymbol(diamond) msize(large) ciopts(recast(rbar) barwidth(0.05)) offset(-.05)), ///
					vertical ///
					xlabel(1 "0" 2 " " 3 " " 4 " " 5 " " 6 ".5" 7 " " 8 " " 9 " " 10 " " 11 "1" , labsize(large) angle(0) grid) ///
					yscale(range(0.7  1.0)) ylabel(.65 (0.05) 1.0, labsize(large)) byopts(legend(off)) ///
					ytitle("Predicted probabilities of turnout", size(large)) ///
					bylabels("Ethnocultural Conception" "Civic Conception") ///			
					ysize(2.75) ///
					subtitle(, size(vlarge)) 
					addplot 1: ,text(0.97 0.8 "2016/17", color(gray) place(right)) norescaling
					addplot 1: ,text(0.8 0.8 "2015", place(right)) norescaling
		
*********************************************************************************
* Figure 2																		*
*********************************************************************************		
	estimates clear

* Logistic regressions

	*2015
	mlogit pi_rs ethn_fs1 civ_fs1 pint_rs age10 i.educat female ost tra uni ben sec con [pweight = weight] if survey == 6
	estimate store m1

	*2017
	mlogit pi_wkp2 ethn_fs1 civ_fs1 pint_wkp age10 i.educat female ost tra uni ben sec con [pweight = weight] if survey == 1
	estimate store m2

* AMEs
	* AMEs 2015

         matrix matrix2015ethn = J(8,3,.) // 3 column for lower ci, estimate, upper ci
         matrix matrix2015civ = J(8,3,.) // 3 column for lower ci, estimate, upper ci
		 foreach n of numlist 1/8 {
         estimates restore m1
         margins, dydx(ethn_fs1 civ_fs1) predict(outcome(`n')) post 
         mat r=r(table)
         matrix list r
         matrix matrix2015ethn[`n', 1] = r[1,1]
         matrix matrix2015ethn[`n', 2] = r[5,1]
         matrix matrix2015ethn[`n', 3] = r[6,1]
         matrix matrix2015civ[`n', 1] = r[1,2]
         matrix matrix2015civ[`n', 2] = r[5,2]
         matrix matrix2015civ[`n', 3] = r[6,2]
         }

	* AMEs 2016/17
         matrix matrix2016ethn = J(8,3,.) // 3 column for lower ci, estimate, upper ci
         matrix matrix2016civ = J(8,3,.) // 3 column for lower ci, estimate, upper ci
         foreach n of numlist 1/8 {
          estimates restore m2
          margins, dydx(ethn_fs1 civ_fs1) predict(outcome(`n')) post 
          mat r=r(table)
          matrix list r
          matrix matrix2016ethn[`n', 1] = r[1,1]
          matrix matrix2016ethn[`n', 2] = r[5,1]
          matrix matrix2016ethn[`n', 3] = r[6,1]
          matrix matrix2016civ[`n', 1] = r[1,2]
          matrix matrix2016civ[`n', 2] = r[5,2]
          matrix matrix2016civ[`n', 3] = r[6,2]
          }
		  
	matrix list matrix2015ethn
	matrix list matrix2015civ
	matrix list matrix2016ethn
	matrix list matrix2016civ	
	
* Coef-Plot 
	coefplot	(matrix(matrix2015ethn[,1]), ci((matrix2015ethn[,2] matrix2015ethn[,3])) msymbol(circle) msize(large) ciopts(recast(rbar) barwidth(0.02)) offset(.20)) /// 
				(matrix(matrix2016ethn[,1]), ci((matrix2016ethn[,2] matrix2016ethn[,3])) msymbol(diamond) msize(large) ciopts(recast(rbar) barwidth(0.02)) offset(-.20)) ///
				||(matrix(matrix2015civ[,1]), ci((matrix2015civ[,2] matrix2015civ[,3])) msymbol(circle) msize(large) ciopts(recast(rbar) barwidth(0.02)) offset(.2)) ///
				(matrix(matrix2016civ[,1]), ci((matrix2016civ[,2] matrix2016civ[,3])) msymbol(diamond) msize(large) ciopts(recast(rbar) barwidth(0.02)) offset(-.2)), ///
				order(r6 r1 r5 r2 r3 r4 r7 r8) ///
				xlabel("-.4" (.1) ".5", labsize(large) angle(0)) ///
				xline(0, lcolor(black)) ///
				byopts(legend(off)) ///
				ysize(2.75) ///
				bylabels("Ethnocultural Conception" "Civic Conception") ///
				subtitle(, size(vlarge)) ///
				coeflabels(r1="CDU/CSU" r2="SPD" r3="Greens" r4="Left" r5="FDP" r6="AfD" r7="Other PI" r8="No PI", labsize(large)) ///
				name(g1, replace)
				addplot 1: ,text(1.2 0.05 "2016/17", color(gray) place(right)) norescaling
				addplot 1: ,text(0.8 0 "2015", place(right)) norescaling
					
				
				
* Logistic Regressions

	*2015
	mlogit vote_rs ethn_fs1 civ_fs1 pint_rs age10 i.educat female ost tra uni ben sec con [pweight = weight] if survey == 6
	estimate store m1

	mlogit vote_rs ethn_fs1 civ_fs1 i.pi_rs pint_rs age10 i.educat female ost [pweight = weight] if survey == 6
	estimate store m2									// PI Control

	*2016/17
	mlogit vote_wkp  ethn_fs1 civ_fs1 pint_wkp age10 i.educat female ost tra uni ben sec con [pweight = weight] if survey == 1
	estimate store m3

	mlogit vote_wkp ethn_fs1 civ_fs1 i.pi_wkp pint_wkp age10 i.educat female ost [pweight = weight] if survey == 1
	estimate store m4									// PI Control

	

* AMEs
	* AMEs 2015
         matrix matrix2015ethn = J(6,3,.) // 3 column for lower ci, estimate, upper ci
         matrix matrix2015civ = J(6,3,.) // 3 column for lower ci, estimate, upper ci
		 foreach n of numlist 1/6 {
         estimates restore m1
         margins, dydx(ethn_fs1 civ_fs1) predict(outcome(`n')) post 
         mat r=r(table)
         matrix list r
         matrix matrix2015ethn[`n', 1] = r[1,1]
         matrix matrix2015ethn[`n', 2] = r[5,1]
         matrix matrix2015ethn[`n', 3] = r[6,1]
         matrix matrix2015civ[`n', 1] = r[1,2]
         matrix matrix2015civ[`n', 2] = r[5,2]
         matrix matrix2015civ[`n', 3] = r[6,2]
         }
	
	* AMEs 2016/17
         matrix matrix2016ethn = J(6,3,.) // 3 column for lower ci, estimate, upper ci
         matrix matrix2016civ = J(6,3,.) // 3 column for lower ci, estimate, upper ci
         foreach n of numlist 1/6 {
          estimates restore m3
          margins, dydx(ethn_fs1 civ_fs1) predict(outcome(`n')) post 
          mat r=r(table)
          matrix list r
          matrix matrix2016ethn[`n', 1] = r[1,1]
          matrix matrix2016ethn[`n', 2] = r[5,1]
          matrix matrix2016ethn[`n', 3] = r[6,1]
          matrix matrix2016civ[`n', 1] = r[1,2]
          matrix matrix2016civ[`n', 2] = r[5,2]
          matrix matrix2016civ[`n', 3] = r[6,2]
          }
		  
		matrix list matrix2015ethn
		matrix list matrix2015civ	  
		matrix list matrix2016ethn
		matrix list matrix2016civ	
	
* Coef-Plot 
	*Ethnocultural norms
	coefplot	(matrix(matrix2015ethn[,1]), ci((matrix2015ethn[,2] matrix2015ethn[,3])) msymbol(circle) msize(large) ciopts(recast(rbar) barwidth(0.02)) offset(.20)) ///
				(matrix(matrix2016ethn[,1]), ci((matrix2016ethn[,2] matrix2016ethn[,3])) msymbol(diamond) msize(large) ciopts(recast(rbar) barwidth(0.02)) offset(-.20)) ///
				||(matrix(matrix2015civ[,1]), ci((matrix2015civ[,2] matrix2015civ[,3])) msymbol(circle) msize(large) ciopts(recast(rbar) barwidth(0.02)) offset(.2)) ///
				(matrix(matrix2016civ[,1]), ci((matrix2016civ[,2] matrix2016civ[,3])) msymbol(diamond) msize(large) ciopts(recast(rbar) barwidth(0.02)) offset(-.2)), ///
				xlabel("-.4" (.1) ".5", labsize(large) angle(0)) ///
				xline(0, lcolor(black)) ///
				byopts(legend(off)) ///
				ysize(2.75) ///
				subtitle(, size(vlarge)) ///
				bylabels("Ethnocultural Conception" "Civic Conception") ///
				coeflabels(r1="CDU/CSU" r2="SPD" r3="Greens" r4="Left" r5="FDP" r6="AfD", labsize(large)) ///
				order(r6 r1 r5 r2 r3 r4) ///
				name(g2, replace) 

				

*********************************************************************************
* Table 2																		*
*********************************************************************************

	estimates clear

* OLS Regressions
	* CDU 2017
	reg  cdu8 ethn_fs1 civ_fs1 pint_wkp age10 i.educat female ost tra uni ben sec con  [pweight = weight] if survey == 1
	estimates store m1
	
	* CSU 2017			
	reg csu8 ethn_fs1 civ_fs1 pint_wkp age10 i.educat female ost tra uni ben sec con  [pweight = weight] if survey == 1
	estimates store m2
	
	* Merkel 2015
	reg w2_merkel_r ethn_fs1 civ_fs1 pint_rs age10 i.educat female ost tra uni ben sec con [pweight = weight] if survey == 6
	estimate store m3

	* Merkel 2016/17
	reg merkel8 ethn_fs1 civ_fs1 pint_wkp age10 i.educat female ost tra uni ben sec con [pweight = weight] if survey == 1
	estimate store m4

	* Table
	esttab 	m1 m2 m3 m4 using "Table_2.rtf", replace b(2)se(2) nobaselevels ///
			stats(r2_a N,fmt(2 0) labels("Adjusted-R2" "N")) ///
			drop(age10 2.educat 3.educat female ost _cons pint_wkp pint_rs) ///
			nonumbers mtitles("CDU 2016/17""CSU 2016/17""Merkel 2015""Merkel 2016/17") ///
			coeflabels(ethn_fs1 "Ethnic conception" civ_fs1 "Civic conception") ///
			addnotes(	"Entries are coefficients from linear regressions; additional controls (not shown): political interest, and sociodemographics") ///
			


	foreach n of numlist 1/4 {
		estimates restore m`n'
		margins, dydx(ethn_fs1 civ_fs1) post
	}





